<?php
namespace app\api\model;
use think\Db;
/**
 * Description of Api
 * 
 * @author gaolei<1455209597@qq.com>
 */
class Api extends \think\Model{
    
    private static $smsKey='bf1a017f5e23510088b60dafc633354d';
    private static $smsUrl='https://sms.yunpian.com/v2/sms/single_send.json';
            public static $valite=[
        'getAd'=>['ad_key'],
        'getPage'=>['page_key'],
        'getNotice'=>['offset','per_page'],
        'getPhoneCode'=>['phone'],
        'checkUser'=>['username'],        
        'registPwd'=>['username','password'],
        'registCode'=>['username','code'],
        'backPassword'=>['username','code','newpassword'],
         //文章接口
         'getArticleList'=>['offset','page'],
          'getArticleInfo'=>['article_id'],
        //用户相关
         'copyArticle'=>['from_aid','token'],
               'searchArticle'=>['key'],
                'comArticle'=>['token','article_id','content'],
                'delComArticle'=>['comment_id','token'],
                'addArticleLike'=>['token','article_id'],
                'concelArticleLike'=>['token','article_id'],
                'concelArticleCatch'=>['token','article_id'],
                'addArticleCatch'=>['token','article_id'],
                'getMyArticle'=>['token'],
                'getMyComment'=>['token','offset','page'],
                'updateArticle'=>['token','title','category_id','content','keywords'],
                'changeUserDetail'=>['token'],
                'changeUserName'=>['token','newusername','code','username'],
                'getMylike'=>['token'],
                'getMyCatch'=>['token'],
                'QueFeedback'=>['token','content'],
                'getArticleComment'=>['article_id','offset','page'],
                'getAboutArticle'=>['cat_id'],
                'changeNickname'=>['token','nickname'],
                'setPassword'=>['token','password','repassword'],
                'webRegist'=>['username','password','repassword','identify_code'],
                'searchUserArticle'=>['token','offset','page'],
                'getUserShareMessage'=>['article_id'],
                'bindUserPhone'=>['username','login_type','code','login_code'],
    ];
    /**
     * 验证短信验证码
     */
    public static function checkCode($phone,$code){
        $res=Db::name('identify_code')->where(['phone'=>$phone,'code'=>$code])->value('id');
        return $res?true:false;
    }
   /**
    * 更新用户信息 
    */
    public static function  updateUserLogin($token){
        $update=[
            'last_login_time'=> time(),
            'last_login_ip'=>$_SERVER['REMOTE_ADDR'], 
        ];
        Db::name('user')->where(['token'=>$token])->setInc('login');
        return Db::name('user')->where(['token'=>$token])->update($update);
    }
    /**
     * 发送短信验证码
     */
    public static function sendSms($phone,$code){
        $data=[
            'apikey'=> self::$smsKey,
            'mobile'=>$phone,
            'text'=>'【晓娱APP】您的验证码是'.$code.'。如非本人操作，请忽略本短信',
        ];
        $res= json_decode(self::curlReq(self::$smsUrl, $data),true);
        return ($res['code']==0)?true:false;
    }
    /*
    * 公共请求方法改进版
    * @author 高磊
    */
    public static function curlReq($urlpath, $data=''){
        //云信加密数据拼接
        $header=[];
        $header[]='Accept:application/json;charset=utf-8';
        $header[]="Content-Type:application/x-www-form-urlencoded;charset=utf-8";     
        //获取云信链接
        $url= $urlpath;
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, $url); 
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header); 
        curl_setopt($ch, CURLOPT_HEADER,0); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
        curl_setopt($ch, CURLOPT_POST, 1); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        $res = curl_exec($ch); 
        curl_close($ch);
        return $res;
    }
}
